/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proyectomain;

/**
 *
 * @author Jordan
 */
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.obex.*;
//import java.util.logging.Level;
//import java.util.logging.Logger;

public class ObexPutClient{

    public static void main(String[] args) throws IOException, InterruptedException {

        String serverURL = null; // = "btgoep://0019639C4007:6";
        if ((args != null) && (args.length > 0)) {
            serverURL = args[0];
        }
        if (serverURL == null) {
            String[] searchArgs = null;
            // Connect to OBEXPutServer from examples
            // searchArgs = new String[] { "11111111111111111111111111111123" };
            ServicesSearch.servicios(searchArgs);
            if (ServicesSearch.serviceFound.size() == 0) {
                System.out.println("OBEX service not found");
                return;
            }
            // Select the first service found
            serverURL = (String)ServicesSearch.serviceFound.elementAt(0);
        }

        System.out.println("Connecting to " + serverURL);

        ClientSession clientSession = (ClientSession) Connector.open(serverURL);
        
        HeaderSet hsConnectReply = clientSession.connect(null);
        if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
            System.out.println("Failed to connect");
            return;
        }

        HeaderSet hsOperation = clientSession.createHeaderSet();
//        hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
//        hsOperation.setHeader(HeaderSet.TYPE, "text");

        hsOperation.setHeader(HeaderSet.NAME, "Bienvenido.jpg");
        hsOperation.setHeader(HeaderSet.TYPE, "image");


        File file = new File("bluetooth-logo.jpg");
        FileInputStream fis = new FileInputStream(file);
//        if(file.exists()){
//        System.out.println("esta bn el directorio");}
//        else{System.out.println("no esta bn");}
        //InputStream in = resource.openStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); //no doubt here is 0
                //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
                //System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            //Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex);
        }
        byte[] bytes = bos.toByteArray();

        //Create PUT Operation
        Operation putOperation = clientSession.put(hsOperation);

        // Send some text to server
//        byte data[] = "Estamos en el laboratorio Curiñanco!!".getBytes("iso-8859-1");
        byte data[] = bytes;
        OutputStream os = putOperation.openOutputStream();
        os.write(data);
        os.close();

        putOperation.close();

        clientSession.disconnect(null);

        clientSession.close();
    }
}

